#include "TelephoneInfo.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

struct TelephoneInfo
{
    int id;
    char *name;
    char *phone;
};

TelInfo *CreateTelInfo()
{
    TelInfo *s = (TelInfo *)malloc(sizeof(TelInfo));
    if (s == NULL)
    {
        printf("CreateTelInfo malloc error!\n");
        exit(-1);
    }
    char name[20];
    printf("请输入用户姓名：\n");
    scanf("%s", name);
    char phone[12];
    printf("请输入用户电话号码：\n");
    scanf("%s", phone);
    s->name = strdup(name); 
    s->phone = strdup(phone);
    return s;
}

TelInfo *CreateFIleTelInfo(FILE *f)
{
    TelInfo *s = (TelInfo *)malloc(sizeof(TelInfo));
    if (s == NULL)
    {
        printf("CreateTelInfo malloc error!\n");
        exit(-1);
    }
    char name[20];
    char phone[12];
    fscanf(f,"name=%s tele=%s\n",name,phone);
    s->name = strdup(name); 
    s->phone = strdup(phone);
}   

void PrintTelInfo(void *p)
{
    TelInfo *s = (TelInfo *)p;
    printf("姓名：%s 电话号码：%s\n", s->name, s->phone);
}

void ClearTeleInfo(void *p)
{
    TelInfo *s = (TelInfo *)p;
    free(s->name);
    free(s->phone);
    free(s);
}
bool IsEqual(void *p1, void *p2)
{
    TelInfo *s = (TelInfo *)p1;
    const char *value = (const char *)p2;
    return (strcmp(s->name, value) == 0 || strcmp(s->phone, value) == 0);
}

int Namecmp(void *p1,void *p2)
{
   
    TelInfo *s1=(TelInfo *)p1;
     return strcmp(s1->name,(const char*)p2);
}

bool BiggerName(void *p1,void*p2)
{
     TelInfo *s1=( TelInfo*)p1;
     TelInfo *s2=( TelInfo*)p2;
   return strcmp(s1->name,s2->name)>0;
}
void save(void* p,FILE* f){
    TelInfo *infrm=(TelInfo*)p;
    fprintf(f,"name=%s tele=%s\n",infrm->name,infrm->phone);
}
